package com.errol.batchprocessor.payroll.advisorimpl.common;

import com.renjia.constant.Constant;
import com.renjia.service.BusDemandService;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeBatchPayload;
import com.renjia.service.module.payroll.advisor.upload.PayrollUploadCreateAdvisor;
import com.renjia.service.module.payroll.advisor.upload.PayrollUploadPostAdvisor;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadBatchPayload;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadSinglePayload;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

/**
 * 任务相关扩展
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/03/17
 * @since v5.0.6
 **/
@Service
public class DemandAdvisorImpl implements
        PayrollUploadCreateAdvisor, PayrollUploadPostAdvisor {

    private final BusDemandService busDemandService;

    public DemandAdvisorImpl(BusDemandService busDemandService) {
        this.busDemandService = busDemandService;
    }

    @Override
    public boolean shallProcess(@NonNull PayrollEmployeeBatchPayload advisorPayload) {
        return true;
    }

    @Override
    public int priority() {
        return 203;
    }

    @Override
    public boolean doAfterNewPayrollRecordAtUploadStage(@NonNull PayrollUploadBatchPayload batchPayload) {
        batchPayload.getBatchEntity().setDemandCategoryId(batchPayload.getBusDemand().getCategoryId());
        batchPayload.getBatchEntity().setDemandCategoryName(batchPayload.getBusDemand().getCategoryName());
        batchPayload.getBatchEntity().setDemandContent(batchPayload.getBusDemand().getContent());
        batchPayload.getBatchEntity().setDemandId(batchPayload.getBusDemand().getId());
        batchPayload.getBatchEntity().setDemandName(batchPayload.getBusDemand().getName());
        batchPayload.getBatchEntity().setDemandResult(batchPayload.getBusDemand().getResult());
        batchPayload.getBatchEntity().setPublishFlag(Constant.COMMON_ONE);
        // v4.6.7.1 添加计价规则
        batchPayload.getBatchEntity().setPriceRule(batchPayload.getBusDemand().getPriceRule());
//        if (batchPayload.getBusDemand() == null) {
//            batchPayload.getBatchEntity().setPublishFlag(Constant.COMMON_ZERO);
//        }
        return true;
    }

    @Override
    public boolean doAfterNewPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        singlePayload.getBusPayrollRecordDetail().setDemandCategoryId(batchPayload.getBatchEntity().getDemandCategoryId());
        singlePayload.getBusPayrollRecordDetail().setDemandCategoryName(batchPayload.getBatchEntity().getDemandCategoryName());
        singlePayload.getBusPayrollRecordDetail().setDemandContent(batchPayload.getBatchEntity().getDemandContent());
        singlePayload.getBusPayrollRecordDetail().setDemandId(batchPayload.getBatchEntity().getDemandId());
        singlePayload.getBusPayrollRecordDetail().setDemandName(batchPayload.getBatchEntity().getDemandName());
        singlePayload.getBusPayrollRecordDetail().setDemandResult(batchPayload.getBatchEntity().getDemandResult());
        singlePayload.getBusPayrollRecordDetail().setPublishFlag(Constant.COMMON_ONE);
        // v4.6.7.1 添加计价方式
        singlePayload.getBusPayrollRecordDetail().setPriceRule(batchPayload.getBatchEntity().getPriceRule());
        return true;
    }

    @Override
    public boolean doBeforeModifyPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        return true;
    }

    @Override
    public boolean doAfterPayrollSavedAtUploadStage(@NonNull PayrollUploadBatchPayload batchPayload) {
        //v4.7.6新增
        busDemandService.onUploadPayrollRecord(batchPayload.getBatchEntity().getDemandId());
        return true;
    }
}
